package io.sage.modules.statistics.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import io.sage.modules.statistics.vo.BStatisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.sage.common.utils.CRC16;
import io.sage.modules.sensor.entity.SysSensorConfiguration;
import io.sage.modules.sensor.service.SensorConfigurationService;
import io.sage.modules.statistics.dao.BStatisticsSoilWaterMapper;
import io.sage.modules.statistics.entity.BStatisticsSoilWater;
import io.sage.modules.statistics.service.BStatisticsSoilWaterService;

/**
 * 传感器土壤含水量服务
 * @author xjh
 *
 */
@Service("statisticsSoilWaterService")
public class BStatisticsSoilWaterServiceImpl implements BStatisticsSoilWaterService {

	@Autowired
	private BStatisticsSoilWaterMapper statisticsSoilWaterMapper;

	@Autowired
	private SensorConfigurationService sensorConfigurationService;

	/**
	 * 土壤列表
	 * @param example
	 * @return
	 */
	@Override
	public List<BStatisticsSoilWater> selectStatisticsSoilWaterList(Map<String, Object> params) {

		return statisticsSoilWaterMapper.selectStatisticsSoilWaterList(params);
	}

	@Override
	public BStatisticsSoilWater selectById(Integer id) {
		return statisticsSoilWaterMapper.selectByPrimaryKey(id);
	}

	/**
	 * 添加单条传感器土壤水分数据
	 * @param goods
	 * @return
	 */
	@Override
	public int insert(String soilData) {

		int insertSelective = 0;
		//获取CRC16校验值.
		String crc16 = CRC16.CRC16(CRC16.hex_to_ten(soilData.substring(0, 10)));
		System.out.println("土壤的数据crc="+crc16);
		System.out.println("土壤的数据="+soilData.substring(10, 14));
		if(crc16.compareToIgnoreCase(soilData.substring(10, 14))==0){

			String querySensor="07 03 00 02 00 01 25 AC";
			SysSensorConfiguration sensorConfiguration = sensorConfigurationService.selectByquerySensor(querySensor);
			float parseLong = (float)Long.parseLong(soilData.substring(6, 10), 16)/10;
			BStatisticsSoilWater statisticsHumidity=new BStatisticsSoilWater();
			statisticsHumidity.setCreateTime(new Date());
			statisticsHumidity.setIsDeleted(0);
			statisticsHumidity.setIsUsed(1);
			statisticsHumidity.setMachineId(sensorConfiguration.getId());
			statisticsHumidity.setUnit("%");
			statisticsHumidity.setValue((double)parseLong);

			insertSelective = statisticsSoilWaterMapper.insertSelective(statisticsHumidity);

		}

		return insertSelective;
	}

	/**
	 * 批量添加传感器的土壤含水量
	 * @param list
	 * @return
	 */
	@Override
	public int saveSoilWaterList(List<BStatisticsSoilWater> list) {
		return statisticsSoilWaterMapper.saveSoilWaterList(list);
	}

	@Override
	public int deleteBatchIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<BStatisVo> selectListByWarehouseId(Map params) {
		return statisticsSoilWaterMapper.selectListByWarehouseId(params);
	}

}
